﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace objass
{
    /// <summary>
    /// WordSelectorControl.xaml 的交互逻辑
    /// </summary>
    public partial class WordSelectorControl : UserControl
    {
        private readonly List<string> _selectedWords = new List<string>();

        public static readonly DependencyProperty IsSingleSelectionProperty =
            DependencyProperty.Register("IsSingleSelection", typeof(bool), typeof(WordSelectorControl), new PropertyMetadata(false));

        public bool IsSingleSelection
        {
            get { return (bool)GetValue(IsSingleSelectionProperty); }
            set { SetValue(IsSingleSelectionProperty, value); }
        }

        public event RoutedEventHandler SelectionChanged;

        public WordSelectorControl()
        {
            InitializeComponent();
        }

        public void SetWords(IEnumerable<string> words)
        {
            WordsPanel.Children.Clear();
            foreach (var word in words)
            {
                var button = new Button
                {
                    Content = word,
                    Style = (Style)FindResource("WordButtonStyle")
                };
                button.SetValue(IsSelectedProperty, false);
                button.Click += WordButton_Click;
                WordsPanel.Children.Add(button);
            }
        }

        private void WordButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button button)
            {
                var word = button.Content.ToString();
                if (IsSingleSelection)
                {
                    DeselectAllButtons();
                    _selectedWords.Clear();
                    _selectedWords.Add(word);
                    button.SetValue(IsSelectedProperty, true);
                }
                else
                {
                    if (_selectedWords.Contains(word))
                    {
                        _selectedWords.Remove(word);
                        button.SetValue(IsSelectedProperty, false);
                    }
                    else
                    {
                        _selectedWords.Add(word);
                        button.SetValue(IsSelectedProperty, true);
                    }
                }
                SelectionChanged?.Invoke(this, new RoutedEventArgs());
            }
        }

        private void DeselectAllButtons()
        {
            foreach (var child in WordsPanel.Children)
            {
                if (child is Button button)
                {
                    button.SetValue(IsSelectedProperty, false);
                }
            }
        }

        public IEnumerable<string> GetSelectedWords()
        {
            return _selectedWords;
        }

        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.RegisterAttached("IsSelected", typeof(bool), typeof(WordSelectorControl), new PropertyMetadata(false));

        public static bool GetIsSelected(UIElement element)
        {
            return (bool)element.GetValue(IsSelectedProperty);
        }

        public static void SetIsSelected(UIElement element, bool value)
        {
            element.SetValue(IsSelectedProperty, value);
        }
    }
}
